<?php 

    if(class_exists('UtVirtualVault') === false)
    {
        Class UtVirtualVault extends UtItemInfo
        {
            private static $_instance = null;
            private $_VirtualItems = array();
            private $_HexaF = '';
            private $_Hexa0 = '';
            private $_account = '';
            private $_FinalHex = "";

            public function __construct()
            {

            }

            public function __clone()
            {

            }

            public function __destroy()
            {

            }

            public static function getStance()
            {
                if(self::$_instance === null)
                    self::$_instance = new self;

                return self::$_instance;
            }

            public function checkItem($serial)
            {
                $find = 0;
                if($this->_Hexa0 == $serial)
                    $find++;

                if($this->_HexaF == $serial)
                    $find++;

                if($serial == null)
                    $find++;

                    
                return ($find > 0) ? true : false;
            }

            public function setSlot($slot,$free = false)
            {
                if($free === false)
                {
                    $this->_VirtualItems[$slot]['TAKEN'] = false;    
                }
                else
                {
                    $this->_VirtualItems[$slot]['TAKEN'] = true;
                }

            }
            
            public function returnHexBySlot($slot)
            {
                return $this->_VirtualItems[$slot]['VALUE'];
            }

            public function LoadVirtualItems($account,$version)
            {
                if(!empty($account))
                {
                    $this->_account = $account;
                    switch($version)
                    {
                        case 1:
                            $corte = 20;
                            $this->_HexaF = str_repeat("F",$corte);
                            $this->_Hexa0 = str_repeat("0",$corte);
                            break;
                        case 2:
                            $corte = 32;
                            $this->_HexaF = str_repeat("F",$corte);
                            $this->_Hexa0 = str_repeat("0",$corte);
                            break;
                    }


                    $tamanho = str_repeat("F",$tam);
                    $query = mssql_query("SELECT CONVERT(TEXT, CONVERT(VARCHAR(3200), Item)) AS 'Inventory' FROM GneeSite.dbo.VirtualVault WHERE login ='".$this->_account."'");
                    if ($query) 
                    {
                        if ($character = mssql_fetch_object($query)) 
                        {
                            $character->Inventory = strtoupper(bin2hex($character->Inventory));
                            $items = str_split($character->Inventory,$corte);
                            foreach($items as $id => $item)
                            {
                                if($this->checkItem($items[$id]) === true)
                                {
                                    $this->setSlot($id,true);
                                    $this->_VirtualItems[$id]['VALUE'] = $items[$id];
                                }
                                else
                                {
                                    $this->setSlot($id,false);
                                    $this->_VirtualItems[$id]['VALUE'] = $items[$id];
                                }
                            }
                        }
                    }
                    
                    return $this->_VirtualItems;
                }

                else
                {
                    throw new Exception('Account Cannot be empty');
                }
            }
            
            public function checkFree($slot)
            {
                return $this->_VirtualItems[$slot]['TAKEN'];
            }
            
            public function removeItemBySLot($slot)
            {
                $this->_VirtualItems[$slot]['VALUE'] = $this->_HexaF;
                $this->setSlot($slot,true);
            }
            
            public function insertItem($hexa)
            {
                $ok = 0;
                foreach($this->_VirtualItems as $key => $val)
                {
                    if($this->checkFree($key) === true && $this->checkItem($this->_VirtualItems[$key]['VALUE'] == true))
                    {
                        $this->_VirtualItems[$key]['VALUE'] = $hexa;
                        $ok++;
                        break;
                    }
                }  
                return ($ok > 0) ? true : false;
            }
            
            public function Finish()
            {
                $this->updateVirtualVault();
                $size = sizeof($this->_VirtualItems);
                for ($x = 0; $x < $size ; $x++)
                {
                    $this->_FinalHex .= $this->_VirtualItems[$x]['VALUE'];
                }
                return $this->updateVirtualVault();
            }
            
            private function updateVirtualVault()
            {
                return mssql_query("Update GneeSite.dbo.VirtualVault set item=0x".$this->_FinalHex." where login='".$this->_account."'");
            }
            
            public function returnVirtualItems()
            {
                foreach($this->_VirtualItems as $key => $value)
                {
                    if($this->checkItem($value['VALUE']) === false)
                    {
                        echo "<br /><a href=\"?removeVirtual=".$key."\">" . $value['VALUE'] . "</a>";    
                    }
                    
                }
            }
        }

    }

?>